<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="getnameList"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="row">
          <el-table-column label="序号" width="120" align="center" type="index" :index="indexMethod" />
          <el-table-column label="角色名称" width="240" prop="name" align="center" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template v-slot="rows">
              <el-button size="small" @click="getAssignDialog(rows.row.id)">分配权限</el-button>
              <el-button size="small" @click="setedit(rows.row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="remove(rows.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :current-page="params.page"
            :page-size="params.pagesize"
            :total="total"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
      <!-- 弹窗 -->
      <el-dialog :title="list.id ? '编辑角色':'新增角色'" :visible="dialogVisible" @close="AddDialog">
        <el-form ref="addform" label-width="120px" :model="list" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="list.name" style="width:80%" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="list.description" style="width:80%" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div type="flex" style="text-align=center">
            <el-button type="primary" size="small" @click="getdetermine">确定</el-button>
            <el-button size="small" @click="setcancel">取消</el-button>
          </div>
        </template>
      </el-dialog>
      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :visible="showAssignDialog"
        @close="closeAssignDialog"
        @open="addPermissionList"
      >
        <!-- 权限点数据展示 -->
        <el-tree
          ref="tree"
          :data="permissionList"
          :props="{ label: 'name' }"
          :default-expand-all="true"
          :show-checkbox="true"
          :check-strictly="true"
          node-key="id"
        />
        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeAssignDialog">取消</el-button>
            <el-button type="primary" @click="clickAssign">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getrole, getname, getmentList, geteditList, removeList } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import treeArray from '@/utils/transTree'
import { assignPermission } from '@/api/user'
export default {
  name: 'Getset',
  data() {
    return {
      roleId: null,
      // 总条数
      total: 0,
      // 角色列表
      row: [],
      // 分页
      params: {
        // 初始在1页
        page: 1,
        // 一页有5个数据
        pagesize: 5
      },
      list: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'blur' }],
        description: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      dialogVisible: false,
      showAssignDialog: false,
      permissionList: [] // 存储权限数据
    }
  },
  mounted() {
    // 获取
    this.getment()
  },
  methods: {
    async getment() {
      //  获取角色数据的接口赋值
      const { total, rows } = await getrole(this.params)
      this.total = total
      this.row = rows
    },
    getnameList() {
      this.dialogVisible = true
    },
    // 校验表单 调用接口更新数据
    async getdetermine() {
      this.$refs.addform.validate(async valid => {
        if (!valid) return
        if (this.list.id) {
          // 有id就调用编辑
          await geteditList(this.list)
        } else {
          await getname(this.list)
        }
        this.getment()
        this.$message.success(`${this.list.id ? '修改' : '新增'}成功`)
        this.dialogVisible = false
        this.$refs.addform.resetFields()
      })
    },
    // ×号关闭
    AddDialog() {
      this.dialogVisible = false
    },
    // 取消关闭
    setcancel() {
      this.list = {
        name: '',
        description: ''
      }
      this.dialogVisible = false
    },
    // 点击编辑 获取回填
    async setedit(id) {
      console.log(id)
      this.dialogVisible = true
      const res = await getmentList(id)
      this.list = res
    },
    // 分页 	currentPage 改变时会触发
    currentChange(page) {
      this.params.page = page
      this.getment()
    },
    // 删除数据
    remove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await removeList(id)
        this.getment()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    },
    indexMethod(index) {
      return index + 1 + (this.params.page - 1) * this.params.pagesize
    },
    getAssignDialog(id) {
      console.log(id)
      this.showAssignDialog = true
      this.roleId = id
    },
    closeAssignDialog() {
      this.showAssignDialog = false
    },
    // 获取列表
    async  addPermissionList() {
      const res = await getPermissionList()
      this.permissionList = treeArray(res)
      // 数据回填
      const res1 = await getmentList(this.roleId)
      this.$refs.tree.setCheckedKeys(res1.permIds)
    },
    // 分配权限
    async clickAssign() {
      console.log(this.roleId)
      // const ids =
      // console.log(ids)
      await assignPermission({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配成功')
      this.showAssignDialog = false
    }
  }
}
</script>

<style>

</style>
